/*
* @(#)RootOperationManager.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.core;

import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.DmsErrorConstant;
import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsContent;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsLocMaster;
import com.dcivision.dms.bean.DmsPersonalStorageSetup;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.bean.MtmDocumentVersion;
import com.dcivision.dms.dao.DmsContentDAObject;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.dms.dao.DmsVersionDAObject;
import com.dcivision.dms.dao.MtmDocumentVersionDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.MtmUserRecordUserRole;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;

/**
 * <p>Class Name:       RootOperationManager.java    </p>
 * <p>Description:      The class mainly handle the root's operation functionalies, such as insert, edit and delete.</p>
 *
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       01/08/2003
 * @version             $Revision: 1.41 $
 */

public class RootOperationManager {

  public static final String REVISION = "$Revision: 1.41 $";

  public static final String COPY_MODE_STRUCTURE_ONLY = "S";
  public static final String COPY_MODE_WHOLE_TREE = "A";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  private DmsStorageLocationResolver storageLocationResolver = null;
  private DmsDocumentNameConvertor documentNameConvertor = null;
  private SessionContainer sessionContainer=null;
  private Connection conn=null;

  /**
   *  Constructor - Creates a new instance of RootOperationManager
   */
  public RootOperationManager(SessionContainer sessionContainer, Connection conn) {
    this.sessionContainer = sessionContainer;
    this.conn = conn;
  }

  /**
    *  Release - To release the variable in instance of DocumentOperationManager
    */
   public void release () {
     this.sessionContainer = null;
     this.conn = null;
     storageLocationResolver = null;
     documentNameConvertor = null;
   }

  public DmsRoot createDefaultPersonalRootPointForUser() throws ApplicationException {
    return(createDefaultPersonalRootPointForUser(sessionContainer.getUserRecordID()));
  }

  /**
   * Creates the Personal root for the user, include create root, dmsDocument, physical root directory & Content
   * and version.
   *
   * @param      userRecordID  user record ID
   * @return     The DMS Root object after creation of personal root.
   * @throws ApplicationException
   */
  public DmsRoot createDefaultPersonalRootPointForUser(Integer userRecordID) throws ApplicationException {
    int rootLocMaster = 0;
    /*   Get root Location Master  */
    try {
      //log.debug("class = "+SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_LOC_RESOLVER_CLASS));
      this.storageLocationResolver = (DmsStorageLocationResolver)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_LOC_RESOLVER_CLASS)).newInstance();
    } catch (Exception e) {
      log.error("Location Resolver class not found.", e);
      throw new ApplicationException(DmsErrorConstant.LOC_RESOLVER_CLASS_NOT_FOUND);
    }
    Integer targetLocID = storageLocationResolver.resolvePersonalStorageLoc(userRecordID, sessionContainer, conn);
    // create root
    Double storageLimit=this.getStorageLimit(userRecordID);
    
    DmsRoot newRoot = new DmsRoot();
    newRoot.setRootName(DmsOperationConstant.DMS_PERSONAL_ROOT);
    newRoot.setRootType(DmsRoot.PERSONAL_ROOT);
    newRoot.setLocID(targetLocID);
    newRoot.setStorageLimit(storageLimit);
    newRoot.setOwnerID(userRecordID);
    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer, conn);
    newRoot = (DmsRoot) dmsRootDAObject.insertObject(newRoot);
    log.debug("Root created");
    dmsRootDAObject=null;

    // create dmsDocument
    DmsDocument newDocument = new DmsDocument();
    newDocument.setDocumentName(DmsOperationConstant.DMS_PERSONAL_ROOT);
    newDocument.setDocumentType(DmsDocument.FOLDER_TYPE);
    newDocument.setParentID(new Integer(0));
    newDocument.setRootID(newRoot.getID());
    newDocument.setCreateType(DmsOperationConstant.DMS_CREATE_BY_SYSTEM);
    newDocument.setEffectiveStartDate(Utility.getCurrentTimestamp());
    newDocument.setOwnerID(userRecordID);
    newDocument.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    newDocument = (DmsDocument) dmsDocumentDAO.insertObject(newDocument);
    log.debug("Document created");
    dmsDocumentDAO=null;

    //Create physical root directory & Content
    try {
      this.documentNameConvertor = (DmsDocumentNameConvertor)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_NAME_CONVERTOR_CLASS)).newInstance();
    } catch (Exception e) {
      log.error("Name Convertor class not found.", e);
      throw new ApplicationException(DmsErrorConstant.NAME_CONVERTOR_CLASS_NOT_FOUND);
    }
    String convertedName =this.documentNameConvertor.encryptDocumentName(newDocument);

    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DmsLocMaster locMaster = rootRetrievalManager.getTargetLocMasterByLocID(targetLocID);
    String personalFolderPath = locMaster.getLocPath();
    log.debug("personal folder path = "+personalFolderPath);
    File personalFolder = new File(personalFolderPath);
    if (!personalFolder.exists()) {
      personalFolder.mkdirs();
    }
    DmsContent newContent = new DmsContent();
    newContent.setConvertedName(convertedName);
    DmsContentDAObject dmsContentDAO = new DmsContentDAObject(sessionContainer, conn);
    newContent = (DmsContent) dmsContentDAO.insertObject(newContent);
    log.debug("Content created");
    dmsContentDAO=null;

    //create version
    DmsVersion newVersion = new DmsVersion();
    newVersion.setDocumentID(newDocument.getID());
    newVersion.setVersionNumber("1");
    newVersion.setVersionLabel(DmsOperationConstant.DMS_ROOT);
    newVersion.setReferenceNo(DmsOperationConstant.DMS_ROOT);
    newVersion.setDescription(DmsOperationConstant.DMS_ROOT);
    newVersion.setParentID(new Integer(0));
    newVersion.setContentID(newContent.getID());
    newVersion.setItemSize(new Integer(0));
    newVersion.setItemStatus(newVersion.AVAILABLE_STATUS);
    DmsVersionDAObject itemDAO = new DmsVersionDAObject(sessionContainer, conn);
    newVersion = (DmsVersion) itemDAO.insertObject(newVersion);
    log.debug("Version created");
    itemDAO=null;

    MtmDocumentVersion mtmDocumentVersion = new MtmDocumentVersion();
    mtmDocumentVersion.setDocumentID(newDocument.getID());
    mtmDocumentVersion.setVersionID(newVersion.getID());
    MtmDocumentVersionDAObject mtmDocumentVersionDAO = new MtmDocumentVersionDAObject(sessionContainer, conn);
    mtmDocumentVersionDAO.insertObject(mtmDocumentVersion);
    log.debug("MTM Version Document created");
    mtmDocumentVersion=null;

    rootRetrievalManager.release();
    return newRoot;
  }

  /**
   * Creates the public root for the user, include create root, dmsDocument, physical root directory & Content
   * and version.
   *
   * @param dmsRoot       The DMS Root object.
   * @return              The DMS Root object after creation.
   * @throws ApplicationException
   */
  public DmsRoot createPublicRoot(DmsRoot dmsRoot) throws ApplicationException {
    // Create DMS Root
    dmsRoot.setRootType(DmsRoot.PUBLIC_ROOT);

    if (dmsRoot.getOwnerID() == null) {
      dmsRoot.setOwnerID(sessionContainer.getUserRecordID());
    }

    if (dmsRoot.getLocID() == null) {
      /* Get root Location Master */
      try {
        log.debug("class = "+SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_LOC_RESOLVER_CLASS));
        this.storageLocationResolver = (DmsStorageLocationResolver)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_LOC_RESOLVER_CLASS)).newInstance();
      } catch (Exception e) {
        log.error("Location Resolver class not found.", e);
        throw new ApplicationException(DmsErrorConstant.LOC_RESOLVER_CLASS_NOT_FOUND);
      }
      dmsRoot.setLocID(storageLocationResolver.resolvePublicStorageLoc(dmsRoot, sessionContainer, conn));
    }

    if("Y".equals(dmsRoot.getUnlimit())){
    	dmsRoot.setStorageLimit(TextUtility.parseDoubleObj("0"));
    }
    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer, conn);
    DmsRoot newRoot = (DmsRoot)dmsRootDAObject.insertObject(dmsRoot);
    dmsRootDAObject=null;

    // Create DMS Document
    DmsDocument newDocument = new DmsDocument();
    newDocument.setDocumentName(newRoot.getRootName());
    newDocument.setDocumentType(DmsDocument.FOLDER_TYPE);
    newDocument.setDescription(newRoot.getRootDesc());
    newDocument.setParentID(new Integer(0));
    newDocument.setRootID(newRoot.getID());
    newDocument.setCreateType(DmsOperationConstant.DMS_CREATE_BY_SYSTEM);
    newDocument.setEffectiveStartDate(Utility.getCurrentTimestamp());
    newDocument.setOwnerID(dmsRoot.getOwnerID());
    newDocument.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    newDocument = (DmsDocument)dmsDocumentDAO.insertObject(newDocument);
    newRoot.setRootFolderID(newDocument.getID());
    dmsDocumentDAO=null;

    // Create Physical Root Directory & Content
    try {
      this.documentNameConvertor = (DmsDocumentNameConvertor)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_NAME_CONVERTOR_CLASS)).newInstance();
    } catch (Exception e) {
      log.error("Name Convertor class not found.", e);
      throw new ApplicationException(DmsErrorConstant.NAME_CONVERTOR_CLASS_NOT_FOUND);
    }
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DmsLocMaster locMaster = rootRetrievalManager.getTargetLocMasterByLocID(dmsRoot.getLocID());
    String convertedName =this.documentNameConvertor.encryptDocumentName(newDocument);
    String publicFolderPath = locMaster.getLocPath();
    File publicFolder = new File(publicFolderPath);
    if (!publicFolder.exists()) {
      publicFolder.mkdirs();
    }
    DmsContent newContent = new DmsContent();
    newContent.setConvertedName(convertedName);
    DmsContentDAObject dmsContentDAO = new DmsContentDAObject(sessionContainer, conn);
    newContent = (DmsContent)dmsContentDAO.insertObject(newContent);
    dmsContentDAO = null;

    // Create DMS Version
    DmsVersion newVersion = new DmsVersion();
    newVersion.setDocumentID(newDocument.getID());
    newVersion.setVersionNumber("1");
    newVersion.setVersionLabel(DmsOperationConstant.DMS_ROOT);
    newVersion.setReferenceNo(DmsOperationConstant.DMS_ROOT);
    newVersion.setDescription(DmsOperationConstant.DMS_ROOT);
    newVersion.setParentID(new Integer(0));
    newVersion.setContentID(newContent.getID());
    newVersion.setItemSize(new Integer(0));
    newVersion.setItemStatus(newVersion.AVAILABLE_STATUS);
    DmsVersionDAObject itemDAO = new DmsVersionDAObject(sessionContainer, conn);
    newVersion = (DmsVersion)itemDAO.insertObject(newVersion);
    itemDAO=null;

    MtmDocumentVersion mtmDocumentVersion = new MtmDocumentVersion();
    mtmDocumentVersion.setDocumentID(newDocument.getID());
    mtmDocumentVersion.setVersionID(newVersion.getID());
    MtmDocumentVersionDAObject mtmDocumentVersionDAO = new MtmDocumentVersionDAObject(sessionContainer, conn);
    mtmDocumentVersionDAO.insertObject(mtmDocumentVersion);
    mtmDocumentVersionDAO=null;

    rootRetrievalManager.release();
    return newRoot;
  }

  /**
   * Updates the public root.
   *
   * @param dmsRoot     The DMS Root object.
   * @return            The DMS Root object after update.
   * @throws ApplicationException
   */
  public DmsRoot updatePublicRoot(DmsRoot dmsRoot) throws ApplicationException {
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DmsDocument rootFolder = rootRetrievalManager.getRootDocument(dmsRoot.getID());
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    rootFolder.setDocumentName(dmsRoot.getRootName());
    rootFolder.setDescription(dmsRoot.getRootDesc());
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    rootFolder = (DmsDocument)dmsDocumentDAO.updateObject(rootFolder);
    
    // clear root folder permission cache after modify permission / lee 06/12/13 EIP-964
    PermissionManager.firePermissionChanged(GlobalConstant.OBJECT_TYPE_DOCUMENT,rootFolder.getID());
    
    dmsDocumentDAO=null;
    Double activeFileSize=docRetrievalManager.totalFileSizeUnderRoot(GlobalConstant.RECORD_STATUS_ACTIVE,(dmsRoot).getID() );
    Double inactiveFileSize=docRetrievalManager.totalFileSizeUnderRoot(GlobalConstant.RECORD_STATUS_INACTIVE,(dmsRoot).getID() );
    double totalSize=activeFileSize.doubleValue()+inactiveFileSize.doubleValue();

    if("Y".equals(dmsRoot.getUnlimit())){
    	dmsRoot.setStorageLimit(TextUtility.parseDoubleObj("0"));
    }
    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer, conn);
    if("0.0".equals(dmsRoot.getStorageLimit().toString()) || dmsRoot.getStorageLimit().doubleValue()>=totalSize/1024){
      dmsRoot = (DmsRoot)dmsRootDAObject.updateObject(dmsRoot);
    }else{
      throw new ApplicationException(DmsErrorConstant.DOCUMENT_STORED_EXCEED_STORAGE_LIMIT);
    }
    dmsRoot.setRootFolderID(rootFolder.getID());

    dmsRootDAObject=null;
    rootRetrievalManager.release();
    return(dmsRoot);
  }

  /**
   * Delete the public root, marked as delete only.
   *
   * @param dmsRoot     The DMS Root object.
   * @return            The DMS Root object after delete.
   * @throws ApplicationException
   */
  public DmsRoot deletePublicRoot(Integer rootID) throws ApplicationException {
    DmsRoot dmsRoot = new DmsRoot();
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);

    DmsDocument rootFolder = rootRetrievalManager.getRootDocument(rootID);
    docOperationManager.deleteFolder(rootFolder);

    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer, conn);
    dmsRoot = (DmsRoot)dmsRootDAObject.deleteObjectByID(rootID);
    dmsRootDAObject=null;

    docOperationManager.release();
    rootRetrievalManager.release();
    return(dmsRoot);
  }

  /**
   * Deletes the root if there is not contains special folder(s) and docuemnt(s) such as 
   * checkout documents, folder(s)/document(s) no delete permission on it.
   * Marked as delete only.
   *
   * @param rootID      root ID
   * @return            List of the folder(s)/document(s) not deleted, 
   *                    1 element in the List, not allow delete type
   *                    2 element in the list, not allow delete document
   * @throws ApplicationException
   */
  public List deletePublicRootExpectSpecDocument(Integer rootID) throws ApplicationException {
    DmsRoot dmsRoot = new DmsRoot();
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);

    DmsDocument rootFolder = rootRetrievalManager.getRootDocument(rootID);
    //docOperationManager.deleteFolder(rootFolder);
    List notDeleteDocumentList = docOperationManager.deleteFolderExpectSpecDocument(rootFolder);

    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer, conn);    
    
    boolean hasNotDeletedDocument = false;
    if(!Utility.isEmpty(notDeleteDocumentList)){
      for(int j = 0 ; j < notDeleteDocumentList.size() ;j++){
        List tempList = (List)notDeleteDocumentList.get(j);                        
        DmsDocument tmpDocument = (DmsDocument)tempList.get(0);
        String tempDeleteType =  (String)tempList.get(1);
        if("C".equals( tempDeleteType)){
          hasNotDeletedDocument = true;                                            
        }
        if("P".equals( tempDeleteType)){
          hasNotDeletedDocument = true;                                      
        }   
      }    
    }
    
    if(!hasNotDeletedDocument){
        dmsRoot = (DmsRoot)dmsRootDAObject.deleteObjectByID(rootID);
    }
    
    dmsRootDAObject=null;

    docOperationManager.release();
    rootRetrievalManager.release();
    return(notDeleteDocumentList);
  }  
  
  /**
   * Copies the root. There are 2 mode of copy
   * <pre>
   *  1. COPY_MODE_STRUCTURE_ONLY - copy structure only without files
   *  2. COPY_MODE_WHOLE_TREE - copy structure and also files
   * </pre>
   *
   * @param sourceRoot  the source root object
   * @param targetRoot  the target root object
   * @param copyMode    mode of copy, COPY_MODE_STRUCTURE_ONLY or COPY_MODE_WHOLE_TREE
   * @throws ApplicationException
   */
  public void copyPublicRoot(DmsRoot sourceRoot, DmsRoot targetRoot, String copyMode) throws ApplicationException {
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);

    DmsDocument rootFolder = rootRetrievalManager.getRootDocument(sourceRoot.getID());
    List tmpList = null;
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    if (COPY_MODE_STRUCTURE_ONLY.equals(copyMode)) {
      tmpList = dmsDocumentDAO.getListByParentID(rootFolder.getID(), rootFolder.getRootID(), DmsDocument.FOLDER_TYPE);
    } else if (COPY_MODE_WHOLE_TREE.equals(copyMode)) {
      tmpList = dmsDocumentDAO.getListByParentID(rootFolder.getID(), rootFolder.getRootID(), "*");
    }

    if (Utility.isEmpty(tmpList)) {
      return;
    }

    for (int i = 0; i < tmpList.size(); i++) {
      DmsDocument dmsDoc = (DmsDocument)tmpList.get(i);
      /**************************************************************************
       * TODO: Copy Folders and Documents, waiting for DocumentOperationManager
       *       finished the copy functions
       **************************************************************************/

    }
    docOperationManager.release();
    rootRetrievalManager.release();
  }
  
  /**
   * Gets the default storage limit of the root base on the user, group and role
   * storage setting.
   * 
   * @param userRecordID          user record ID 
   * @return Double StorageLimit in Mega bytes         
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsPersonalStorageSetup
   *
   */
  public Double getStorageLimit(Integer userRecordID) throws ApplicationException{
    Double storageLimit=new Double(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_ROOT_STORAGE_LIMIT));
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    List groupList=mtmUserRecordUserGroupDAO.getUserGroupListByUserRecordID(userRecordID);
    List roleList=mtmUserRecordUserRoleDAO.getListByUserRecordID(userRecordID);
    Double roleStorageLimit=new Double(-1);
    for(int i=0;i<roleList.size();i++){
      MtmUserRecordUserRole mtmUerRecordUserRole=(MtmUserRecordUserRole)roleList.get(i);
      DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(mtmUerRecordUserRole.getUserRoleID(),DmsPersonalStorageSetup.MEMBER_TYPE_ROLE);
      if(!Utility.isEmpty(dmsPersonalStorageSetup)){
        if(dmsPersonalStorageSetup.getStorageLimit().intValue()>roleStorageLimit.intValue()){
          roleStorageLimit=dmsPersonalStorageSetup.getStorageLimit(); 
        }
      }
    }
    if(roleStorageLimit.intValue()!=-1){
      storageLimit=roleStorageLimit;   
    }
    Double groupStorageLimit=new Double(-1);
    boolean hasGroup=false;
    for(int i=0;i<groupList.size();i++){
      UserGroup userGroup=(UserGroup)groupList.get(i);
      DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userGroup.getID(),DmsPersonalStorageSetup.MEMBER_TYPE_GROUP);   
      if(!Utility.isEmpty(dmsPersonalStorageSetup)){
        if(dmsPersonalStorageSetup.getStorageLimit().intValue()>groupStorageLimit.intValue()){
          groupStorageLimit = dmsPersonalStorageSetup.getStorageLimit();
          hasGroup=true;
        }
      }else if(hasGroup==false){
        List newGroupList=new ArrayList();
        newGroupList.add(0,userGroup.getID());
        List newRoleList=mtmUserGroupUserRoleDAO.getListByUserGroupAry(newGroupList);
        for(int j=0;j<newRoleList.size();j++){
          MtmUserGroupUserRole mtmUerGroupUserRole=(MtmUserGroupUserRole)newRoleList.get(j);
          DmsPersonalStorageSetup roleDmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(mtmUerGroupUserRole.getUserRoleID(),DmsPersonalStorageSetup.MEMBER_TYPE_ROLE);
          if(!Utility.isEmpty(roleDmsPersonalStorageSetup)){
            if(roleDmsPersonalStorageSetup.getStorageLimit().intValue()>groupStorageLimit.intValue()){
              groupStorageLimit=roleDmsPersonalStorageSetup.getStorageLimit(); 
            }
          }
        }
      }
    }
    if(groupStorageLimit.intValue()!=-1){
      storageLimit=groupStorageLimit;  
    } 
    return storageLimit;
  }

}
